package com.maserb.monitor.web.jw_question;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.jwQuestion.JwQuestionEntity;
import com.maserb.monitor.entity.jwQuestion.JwQuestionItemEntity;
import com.maserb.monitor.enums.EducationInfoTypeEnum;
import com.maserb.monitor.enums.JwQuestionItemTypeEnum;
import com.maserb.monitor.enums.JwQuestionTypeEnum;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.jwQuestion.JwQuestionItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Controller
@RequestMapping("/jwQuestionItem")
public class JwQuestionItemController {

    @Autowired
    JwQuestionItemService jwQuestionItemService;

    @Autowired
    private IToolService toolService;

    @RequestMapping("/doListQuestionItem")
    @ResponseBody
    public PageResponseResult<JwQuestionItemEntity> doListPointCategoryItem(PageRequestParam page, final String parentId) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<JwQuestionItemEntity>>() {
            @Override
            public Page<JwQuestionItemEntity> invoke(Pageable var1) throws Exception {
                Page<JwQuestionItemEntity> page = jwQuestionItemService.findAllByParentId(parentId, var1);
                for(JwQuestionItemEntity entity:page.getContent()){
                    entity.setCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                    if(null!=entity.getType() && !"".equals(entity.getType())) {
                        try{
                            entity.setType(JwQuestionItemTypeEnum.getNameByValue(Integer.parseInt(entity.getType())));
                        }catch (NumberFormatException e){
                            entity.setType("未知");
                        }
                    }
                }
                return page;
            }
        });
    }

    @RequestMapping("/getAddQuestionItem/{parentId}")
    public ModelAndView getAddQuestionItem(@PathVariable String parentId) {
        ModelAndView result = new ModelAndView();
        JwQuestionItemTypeEnum[] type = JwQuestionItemTypeEnum.values();
        result.addObject("type",type);
        result.addObject("parentId", parentId);
        result.setViewName("jw_question/addItem");
        return result;
    }

    @RequestMapping("/doAddQuestionItem")
    @ResponseBody
    public AjaxResult<Boolean> doAddQuestionItem(final HttpServletRequest request, JwQuestionItemEntity entity, String parentId) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                entity.setCreateTime(CreateTimeUtils.getCreateTime());
                jwQuestionItemService.save(entity, parentId);
                return true;
            }
        });
    }

    @RequestMapping("/getUpdateItem/{id}")
    public ModelAndView getUpdateItem(@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        JwQuestionItemEntity entity = jwQuestionItemService.findOne(id);
        result.setViewName("jw_question/item_edit");
        JwQuestionItemTypeEnum[] type = JwQuestionItemTypeEnum.values();
        result.addObject("type",type);
        result.addObject("entity", entity);
        return result;
    }

    @RequestMapping("/doUpdateItem")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateItem(final HttpServletRequest request, JwQuestionItemEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                JwQuestionItemEntity jwQuestionItemEntity = jwQuestionItemService.findOne(entity.getId());
                jwQuestionItemEntity.setName(entity.getName());
                jwQuestionItemEntity.setType(entity.getType());
                jwQuestionItemEntity.setDescription(entity.getDescription());
                jwQuestionItemEntity.setUpdateTime(CreateTimeUtils.getCreateTime());
                jwQuestionItemEntity.setScore(entity.getScore());
                jwQuestionItemService.save(jwQuestionItemEntity);
                return true;
            }
        });
    }

    @RequestMapping("/doDeleteItem")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteItem(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                keys.forEach(id -> jwQuestionItemService.doDelete(id));
                return true;
            }
        });
    }

}
